package com.woniuxy.factory.service.imp;

import com.woniuxy.factory.entity.Report;
import com.woniuxy.factory.exception.BusinessException;
import com.woniuxy.factory.mapper.ReportMapper;
import com.woniuxy.factory.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ReportServiceImp implements ReportService {

    @Autowired(required = false)
    ReportMapper rm;

    //在插入保单之前，查询当前计划单的报工单是否已经生成
    @Override
    public void selectReportByPlanid(String planid) {
        //判断是否已完成
        int state = rm.selectScheduleState(planid);
        if(state==0){
            throw new BusinessException("该计划单还未完成，不能生成报工单");
        }
        Report r = rm.selectReportByPlanid(planid);
        //判断是否为空
        if(r!=null){
            throw new BusinessException("该计划单已生成报工单，不可重复操作");
        }
    }

    //新增报工单
    @Override
    public void insertReport(Report report) {
        //根据生产的合格数和报废数可以计算出当前计划单所生产的总数量
        report.setProduceSum(report.getRepEligible()+report.getRepScrap());
        rm.insertReport(report);
    }

    //查询所有报工单
    @Override
    public List<Report> selectAllReport(Report report, int page, int limit) {
        //设置起始页
        page = (page-1)*limit;
        List<Report> lr = rm.selectAllReport(report,page,limit);
        return lr;
    }

    //查询报工单总数
    @Override
    public int selectReportCounts(Report report) {
        int count = rm.selectReportCounts(report);
        return count;
    }

    //删除
    @Override
    public void deleteReportById(String repId) {
        rm.deleteReportById(repId);
    }

    //通过id查询数据回显
    @Override
    public Report reportEcho(String repId) {
        Report r = rm.reportEcho(repId);
        return r;
    }

    //修改
    @Override
    public void updateReportById(Report report) {
        rm.updateReportById(report);
    }

}
